package com.klikmee.sms.core.base.postman;

import java.text.MessageFormat;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.base.EventManager;
import com.klikmee.sms.core.base.dao.BulkMessageDAO;
import com.klikmee.sms.core.base.dao.OutgoingMessageDAO;
import com.klikmee.sms.core.base.util.States;
import com.klikmee.sms.core.commons.events.AppEvent;
import com.klikmee.sms.core.commons.events.EventListener;
import com.klikmee.sms.core.model.BulkMessage;
import com.klikmee.sms.core.model.OutgoingMessage;

public class ServiceMessageDelivererEventListenerImpl implements EventListener {

	// logger instance
	private Logger	logger	= Logger.getLogger( ServiceMessageDelivererEventListenerImpl.class );

	/**
	 * Dao used to access outgoing messages
	 */
	private OutgoingMessageDAO outgoingMessageDAO;
	
	/**
	 * Dao used to access bulk messages
	 */
	private BulkMessageDAO bulkMessageDAO;
	
	@Override
	public void handleEvent(AppEvent appEvent) {
		String eventType = appEvent.getEventType();

		if (eventType.equals( EventManager.INCOMING_MESSAGE_DELIVERED_TO_SERVICE )) {
			onIncomingMessageDeliveredToService( appEvent );
		}
		else if (eventType.equals( EventManager.DELIVERY_REPORT_DELIVERED_TO_SERVICE )) {
			onDeliverReportDeliveredToService( appEvent );
		}
		else if (eventType.equals( EventManager.BULK_DELIVERY_REPORT_DELIVERED_TO_SERVICE )) {
			onBulkDeliveryReportDeliveredToService(appEvent);
		}

	}

	private void onBulkDeliveryReportDeliveredToService(AppEvent appEvent) {
		// extract bulk message from event
		BulkMessage bulkMessage = appEvent.<BulkMessage>getData();
		// update bulk message state depending on current state
		// if current state is delivered, update it to DELIVERED_SERVICE_NOTIFIED
		switch (bulkMessage.getState()) {
		case States.BulkMessage.DELIVERED:
			bulkMessage.setState( States.BulkMessage.DELIVERED_SERVICE_NOTIFIED );
			break;
			// if it's undelivered upadte it to UNDELIVERED_SERVICE_NOTIFIED
		case States.BulkMessage.UNDELIVERED:
			bulkMessage.setState( States.BulkMessage.UNDELIVERED_SERVICE_NOTIFIED );
		default:
			logger.warn( MessageFormat.format( "Unexpected state [{0}] of {1}", bulkMessage.getState(), bulkMessage ) );
			break;
		}
		// update bulk message into database
		this.bulkMessageDAO.edit( bulkMessage );
		
		logger.info( MessageFormat.format( "Updated state of {0} to [{1}]", bulkMessage, bulkMessage.getState() ) );
	}

	private void onDeliverReportDeliveredToService(AppEvent appEvent) {
		OutgoingMessage outgoingMessage = appEvent.<OutgoingMessage> getData();
		// if status forwarding was successful update states
		switch (outgoingMessage.getState()) {
		case States.OutgoingMessage.UNDELIVERED:
			outgoingMessage.setState( States.OutgoingMessage.UNDELIVERED_SERVICE_NOTIFIED );
			logger.debug( MessageFormat.format( "Pushed outgoing message state to [{0}]", outgoingMessage.getState() ) );
			break;

		case States.OutgoingMessage.DELIVERED:
			outgoingMessage.setState( States.OutgoingMessage.DELIVERED_SERVICE_NOTIFIED );
			logger.debug( MessageFormat.format( "Pushed outgoing message state to [{0}]", outgoingMessage.getState() ) );
			break;
		default:
			break;
		}
		this.outgoingMessageDAO.edit( outgoingMessage );

	}

	private void onIncomingMessageDeliveredToService(AppEvent appEvent) {
			
	}
	
	public void setOutgoingMessageDAO(OutgoingMessageDAO outgoingMessageDAO) {
		this.outgoingMessageDAO = outgoingMessageDAO;
	}
	
	public void setBulkMessageDAO(BulkMessageDAO bulkMessageDAO) {
		this.bulkMessageDAO = bulkMessageDAO;
	}

}
